﻿using Musi.Core.CSV;

namespace Musi
{
    [System.Serializable]
    public class ChartPreview
    {
        const string SONG_PATTERN = "[=[Song]=]";
        const string CHART_NAME_PATTERN = "[=[Chart]=]";
        const string MAKER_PATTERN = "[=[Maker]=]";
        const string TIME_LENGTH_PATTERN = "[=[Length]=]";
        const string LEVEL_PATTERN = "[=[Level]=]";

        protected ChartSong song;
        protected string name;
        protected string maker;
        protected ChartTime length;
        protected int level;

        public ChartSong Song { get => song; set => song = value; }
        public string Name { get => name; set => name = value; }
        public string Maker { get => maker; set => maker = value; }
        public ChartTime Length { get => length; set => length = value; }
        public int Level { get => level; set => level = value; }

        public void SetSongPath(string path)
        {
            song.path = path;
        }

        public static ChartPreview GetFromCSV(CSVParser csvParser)
        {
            ChartPreview chartPreview = new ChartPreview();
            var pos = csvParser.Find(SONG_PATTERN);
            if(pos != CSVParser.NotFindTarget)
            {
                chartPreview.Song = csvParser.ParseList<ChartSong>(pos.Item1).Unwarp();
            }

            pos = csvParser.Find(CHART_NAME_PATTERN);
            if(pos != CSVParser.NotFindTarget)
            {
                pos.Item2++;
                chartPreview.Name = csvParser.GetDataString(pos);
            }

            pos = csvParser.Find(MAKER_PATTERN);
            if(pos != CSVParser.NotFindTarget)
            {
                pos.Item2++;
                chartPreview.Maker = csvParser.GetDataString(pos);
            }

            pos = csvParser.Find(TIME_LENGTH_PATTERN);
            if (pos != CSVParser.NotFindTarget)
            {
                pos.Item2++;
                chartPreview.Length = csvParser.Parse<ChartTime>(pos).Unwarp();
            }

            pos = csvParser.Find(LEVEL_PATTERN);
            if (pos != CSVParser.NotFindTarget)
            {
                pos.Item2++;
                chartPreview.Level = csvParser.ParseInt(pos).Unwarp();
            }

            return chartPreview;
        }
    }
}
